package com.yanqu.road.server.cmd.player;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.player.QueryPlayerResult;
import com.yanqu.road.exception.BusinessException;
import com.yanqu.road.logic.helper.ActivityHelper;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.DoubleSpringProto;
import com.yanqu.road.pb.decoration.DecorationProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.CmdApi;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Cmd(code = Protocol.S_QUERY_PLAYER_SERVER_BY_USER_ALIAS_ID, desc = "查询玩家ID和区服，根据userAliasId")
@CmdApi(reqMsg = DecorationProto.QueryPlayerUserIdAndServerIdByUserAliasIdReqMsg.class, respMsg = DecorationProto.QueryPlayerUserIdAndServerIdByUserAliasIdRespMsg.class)
public class QueryPlayerUserIdAndServerIdByUserAliasIdCmd extends AbstractCommand {

	private long watchUserAliasId;
	private int activityId;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		DecorationProto.QueryPlayerUserIdAndServerIdByUserAliasIdReqMsg reqMsg = DecorationProto.QueryPlayerUserIdAndServerIdByUserAliasIdReqMsg.parseFrom(packet.getBytes());
		watchUserAliasId = reqMsg.getWatchUserAliasId();
		activityId = reqMsg.hasActivityId() ? reqMsg.getActivityId() : 0;
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		DecorationProto.QueryPlayerUserIdAndServerIdByUserAliasIdRespMsg.Builder respMsg = DecorationProto.QueryPlayerUserIdAndServerIdByUserAliasIdRespMsg.newBuilder();
		try {
			Map<Long, Long> userServerMap = UserMgr.getUserServerByUserAliasId(watchUserAliasId, activityId);
			for (Map.Entry<Long, Long> entry : userServerMap.entrySet()) {
				long userId = entry.getKey();
				long serverId = entry.getValue();
				DecorationProto.PlayerUserIdServerIdTemp.Builder userIdServerIdTemp = DecorationProto.PlayerUserIdServerIdTemp.newBuilder();
				userIdServerIdTemp.setUserId(userId);
				userIdServerIdTemp.setServerId(serverId);
				respMsg.addUserIdServerId(userIdServerIdTemp);
			}
			respMsg.setRet(0);
		} catch (BusinessException e) {
			respMsg.setRet(e.getCode());
		}
		YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.U_QUERY_PLAYER_SERVER_BY_USER_ALIAS_ID, respMsg);
		GamePlayerMgr.sendPacket(player.getUserId(), pbMsg);
	}

	@Override
	public void sendData(GamePlayer player) {

	}
}
